//
//  LEStatisticsSaleViewController.m
//  LoveExpressBusiness
//
//  Created by 刘甲奇 on 2017/5/12.
//  Copyright © 2017年 DFH. All rights reserved.
//

#import "LEStatisticsSaleViewController.h"
#import "LEStatisticsFirstCell.h"
#import "LEStatisticsSecondCell.h"
#import "MakerTools.h"

#import "LEStatisticsLineChartController.h"

static NSString *const LEStatisticsFirstCellId = @"LEStatisticsFirstCell";
static NSString *const LEStatisticsSecondCellId = @"LEStatisticsSecondCell";

@interface LEStatisticsSaleViewController ()

// 标题数组
@property(nonatomic,strong)NSArray *titleArray;

/**
 七天的信息
 */
@property(nonatomic,strong)NSDictionary *sevenDayInfo;

/**
 七天的信息
 */
@property(nonatomic,strong)NSDictionary *yesterdayInfo;
/**
 七天的信息
 */
@property(nonatomic,strong)NSDictionary *todayInfo;
/**
 30天的信息
 */
@property(nonatomic,strong)NSDictionary *monthInfo;

@end

@implementation LEStatisticsSaleViewController

- (void)setupUI
{
    [super setupUI];

    self.title = @"销售额统计";
    [self.tableView registerNib:[UINib nibWithNibName:@"LEStatisticsFirstCell" bundle:nil] forCellReuseIdentifier:LEStatisticsFirstCellId];
    [self.tableView registerNib:[UINib nibWithNibName:@"LEStatisticsSecondCell" bundle:nil] forCellReuseIdentifier:LEStatisticsSecondCellId];
    
}
- (void)loadData
{
    self.titleArray = [NSArray arrayWithObjects:@"七日下单金额",@"昨日下单金额",@"今日下单金额",@"近30下单金额", nil];
    [self requestRefreshData];
}
#pragma mark- 网络请求
- (void)requestRefreshData
{
    //今天
    [self getDataWithStartTime:[MakerTools getDayZeroTimeWith:0] endTime:[MakerTools getCurrentTimestamp]];
    //昨天
    [self getDataWithStartTime:[MakerTools getDayZeroTimeWith:1] endTime:[MakerTools getDayZeroTimeWith:0]];
    //7天
    [self getDataWithStartTime:[MakerTools getDayZeroTimeWith:7] endTime:[MakerTools getCurrentTimestamp]];
    //30天
    [self getDataWithStartTime:[MakerTools getDayZeroTimeWith:30] endTime:[MakerTools getCurrentTimestamp]];
    
}
- (void)getDataWithStartTime:(NSString *)startTime endTime:(NSString *)endTime
{
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setObject:startTime forKey:@"start_time"];
    [dict setObject:endTime forKey:@"end_time"];
    
    [SVProgressHUD showWithStatus:@"正在加载中"];
    __weak typeof (self) weakSelf = self;
    dispatch_group_t group = dispatch_group_create();
    dispatch_group_enter(group);
    [API requestAppendKey:StoreStatisticsUrl params:dict success:^(NSDictionary *successCode) {
        [SVProgressHUD dismiss];
        LELog(@"%s---%d---%@",__func__,__LINE__,successCode);
        id datas = successCode[@"datas"];
        if ([datas isKindOfClass:[NSDictionary class]])
        {
            NSString *error = datas[@"error"];
            if (error)
            {
                [SVProgressHUD showErrorWithStatus:error];
            }
        }else if ([datas isKindOfClass:[NSArray class]])
        {
            if ([startTime isEqualToString:[MakerTools getDayZeroTimeWith:7]])
            {
                weakSelf.sevenDayInfo = datas[0];
                dispatch_group_leave(group);
            }
            if ([startTime isEqualToString:[MakerTools getDayZeroTimeWith:1]])
            {
                weakSelf.yesterdayInfo = datas[0];
                dispatch_group_leave(group);
            }
            
            if ([startTime isEqualToString:[MakerTools getDayZeroTimeWith:0]])
            {
                weakSelf.todayInfo = datas[0];
                dispatch_group_leave(group);
            }
            if ([startTime isEqualToString:[MakerTools getDayZeroTimeWith:30]])
            {
                weakSelf.monthInfo = datas[0];
                dispatch_group_leave(group);
            }
        }
    } falure:^(NSError *er) {
        LELog(@"%s---%d---%@",__func__,__LINE__,er);
        [SVProgressHUD dismiss];
    }];
    
    dispatch_group_notify(group, dispatch_get_main_queue(), ^{
        [self.tableView reloadData];
    });
    
}
#pragma mark- tableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 4;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 115;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if ( indexPath.row%2 == 0)
    {
        LEStatisticsFirstCell *cell = [tableView dequeueReusableCellWithIdentifier:LEStatisticsFirstCellId forIndexPath:indexPath];
        [cell setColor:indexPath];
        [cell setTitle:self.titleArray[indexPath.row]];
        
        if (indexPath.row == 0)
        {
            if(self.sevenDayInfo)
            {
                cell.numberLabel.text = self.sevenDayInfo[@"sum"];
            }
            
        }
        if (indexPath.row == 2)
        {
            if(self.todayInfo)
            {
                cell.numberLabel.text = self.todayInfo[@"sum"];
            }
            
        }
        
        return cell;
    }else{
        LEStatisticsSecondCell *cell = [tableView dequeueReusableCellWithIdentifier:LEStatisticsSecondCellId forIndexPath:indexPath];
        [cell setTitle:self.titleArray[indexPath.row]];
        if (indexPath.row == 1)
        {
            if(self.yesterdayInfo)
            {
                cell.numberLabel.text = self.yesterdayInfo[@"sum"];
            }
            
        }
        if (indexPath.row == 3)
        {
            if(self.monthInfo)
            {
                cell.numberLabel.text = self.monthInfo[@"sum"];
            }
            
        }
        [cell setColor:indexPath];
        return cell;
    }
    
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    LEStatisticsLineChartController *lineChart = [[LEStatisticsLineChartController alloc]init];
    lineChart.mode = SaleStatistic;
    if (indexPath.row == 0) {
        lineChart.start_time = [MakerTools getDayZeroTimeWith:7];
        lineChart.end_time = [MakerTools getCurrentTimestamp];
        lineChart.mode = SaleStatistic;
        lineChart.timeMode = SevenDay;
    }
    if (indexPath.row == 1) {
        lineChart.start_time = [MakerTools getDayZeroTimeWith:1];
        lineChart.end_time = [MakerTools getDayZeroTimeWith:0];
        lineChart.mode = SaleStatistic;
        lineChart.timeMode = Yesterday;
    }
    if (indexPath.row == 2) {
        lineChart.start_time = [MakerTools getDayZeroTimeWith:0];
        lineChart.end_time = [MakerTools getCurrentTimestamp];
        lineChart.mode = SaleStatistic;
        lineChart.timeMode = Today;
    }
    if (indexPath.row == 3) {
        lineChart.start_time = [MakerTools getDayZeroTimeWith:30];
        lineChart.end_time = [MakerTools getCurrentTimestamp];
        lineChart.mode = SaleStatistic;
        lineChart.timeMode = Month;
    }
    [self.navigationController pushViewController:lineChart animated:YES];
}

@end
